﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace AgileShare.Framework.Library.Managers
{
    /// <summary>
    /// Base Property Bag Manager Class
    /// </summary>
    public class PropertyBagManagerBase
    {
        /// <summary>
        /// Sets a property on the SPWeb object.  
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="web">The SPWeb object.</param>
        /// <param name="key">The key for the property.  Converted to lower case.</param>
        /// <param name="value">The value object of the property.</param>
        /// <param name="overwrite">Overwrite flag.  Property value is overwritten if true.</param>
        public static void SetProperty(SPWeb web, string key, object value, bool overwrite)
        {
            if (!string.IsNullOrEmpty(key))
            {
                String lowerKey = key.ToLower();
                // legacy property bag
                if (web.Properties[lowerKey] == null)
                {
                    web.Properties[lowerKey] = value.ToString();
                }
                else
                {
                    if (overwrite)
                    {
                        web.Properties[lowerKey] = value.ToString();
                    }
                }
                // all properties bag
                if (web.AllProperties[lowerKey] == null)
                {
                    web.AllProperties.Add(lowerKey, value);
                }
                else
                {
                    if (overwrite)
                    {
                        web.AllProperties[lowerKey] = value.ToString();
                    }
                }

                SPWebUpdate(web);
            }
        }

        /// <summary>
        /// Sets the property.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="webApp">The web app.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        public static void SetProperty(SPWebApplication webApp, string key, object value, bool overwrite)
        {
            if (webApp.Properties.ContainsKey(key))
            {
                webApp.Properties[key] = value;
            }
            else
            {
                webApp.Properties.Add(key, value);
            }
            webApp.Update();
        }

        /// <summary>
        /// Sets the property.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="farm">The farm.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        public static void SetProperty(SPFarm farm, string key, object value, bool overwrite)
        {
            if (farm.Properties.ContainsKey(key))
            {
                farm.Properties[key] = value;
            }
            else
            {
                farm.Properties.Add(key, value);
            }
            farm.Update();
        }

        /// <summary>
        /// Gets a property from the SPWeb object's property bag.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="web">The SPWeb object.</param>
        /// <param name="key">The key for the property.  Converted to lower case.</param>
        /// <returns></returns>
        public static string GetProperty(SPWeb web, string key)
        {
            string retVal = null;
            if (!string.IsNullOrEmpty(key))
            {
                string lowerKey = key.ToLower();
                if (web.AllProperties.Contains(lowerKey))
                {
                    retVal = web.AllProperties[lowerKey].ToString();
                }
            }
            return retVal;
        }

        /// <summary>
        /// Gets the property.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="webApp">The web app.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string GetProperty(SPWebApplication webApp, string key)
        {
            if (webApp.Properties.ContainsKey(key))
            {
                return webApp.Properties[key].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the property.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="farm">The farm.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string GetProperty(SPFarm farm, string key)
        {
            if (farm.Properties.ContainsKey(key))
            {
                return farm.Properties[key].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Deletes a property from the SPWeb object.  
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="web">The SPWeb object.</param>
        /// <param name="key">The key for the property.  Converted to lower case.</param>
        public static void DeleteProperty(SPWeb web, string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                string lowercaseKey = key.ToLower();
                if (web.AllProperties.Contains(lowercaseKey))
                {
                    web.AllProperties.Remove(lowercaseKey);
                }
                if (web.Properties[lowercaseKey] != null)
                {
                    web.Properties[lowercaseKey] = null;
                    web.Properties.Update();
                }
            }
            SPWebUpdate(web);
        }

        /// <summary>
        /// Deletes the property.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="webApp">The web app.</param>
        /// <param name="key">The key.</param>
        public static void DeleteProperty(SPWebApplication webApp, string key)
        {
            if (webApp.Properties.ContainsKey(key))
            {
                webApp.Properties[key] = null;
                webApp.Properties.Remove(key);
                webApp.Update();
            }
        }

        /// <summary>
        /// Deletes the property.
        /// Ensure caller has sufficient permission or runs with Elevated Permission
        /// </summary>
        /// <param name="farm">The farm.</param>
        /// <param name="key">The key.</param>
        public static void DeleteProperty(SPFarm farm, string key)
        {
            if (farm.Properties.ContainsKey(key))
            {
                farm.Properties[key] = null;
                farm.Properties.Remove(key);
                farm.Update();
            }
        }

        private static void SPWebUpdate(SPWeb oSPWeb)
        {
            if (!oSPWeb.AllowUnsafeUpdates)
            {
                oSPWeb.AllowUnsafeUpdates = true;
                oSPWeb.Update();
                oSPWeb.AllowUnsafeUpdates = false;
            }
            else
            {
                oSPWeb.Update();
            }

        }
    }
}
